﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using System.Drawing;

namespace BSCMFF.Web.UserControls
{
    /// <summary>
    /// Evaluation questions category - eval mode.
    /// </summary>
    /// <remarks></remarks>
    public partial class EvaluationQuestionCategoryEvalMode : System.Web.UI.UserControl
    {
        /// <summary>
        /// Gets or sets the name of the category.
        /// </summary>
        /// <value>The name of the category.</value>
        /// <remarks></remarks>
        public string CategoryName
        {
            get
            {
                return LabelCurrentCategory.Text;
            }
            set
            {
                LabelCurrentCategory.Text = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private List<Evaluation_Question> savedQuestions;
        /// <summary>
        /// Gets or sets the saved questions.
        /// </summary>
        /// <value>The saved questions.</value>
        /// <remarks></remarks>
        public List<Evaluation_Question> SavedQuestions
        {
            get
            {
                return savedQuestions;
            }
            set
            {
                savedQuestions = value;
            }
        }

        /// <summary>
        /// Gets or sets the min.
        /// </summary>
        /// <value>The min.</value>
        /// <remarks></remarks>
        public int Min { get; set; }
        /// <summary>
        /// Gets or sets the max.
        /// </summary>
        /// <value>The max.</value>
        /// <remarks></remarks>
        public int Max { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public bool DisabledValues = false;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            CreateQuestions();
        }

        /// <summary>
        /// Creates the questions.
        /// </summary>
        /// <remarks></remarks>
        public void CreateQuestions()
        {
            Table t = new Table();
            t.ID = "TableQuestions";

            Label l = new Label();
            l.ID = "LabelHeader";
            l.Text = "Questions";

            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.Width = 800;
            tc.Controls.Add(l);

            tr.Cells.Add(tc);


            Label ln = new Label();
            ln.ID = "LabelNumber";// +j.ToString();
            for (int j = Min; j <= Max; ++j)
            {
                ln.Text += "<span style='float:left;width:25px;text-align:center'>" + j.ToString() + "</span>";                
            }
            TableCell tcln = new TableCell();
            tcln.Controls.Add(ln);
            tcln.HorizontalAlign = HorizontalAlign.Center;
            tr.Cells.Add(tcln);
            
            t.Rows.Add(tr);
            
            for (int i = 0; i < savedQuestions.Count; ++i)
            {
                Label lq = new Label();

                lq.ID = "LabelQuestion" + i.ToString();
                lq.Text = savedQuestions[i].Question;
                
                TableRow trQ = new TableRow();
                TableCell tcQ = new TableCell();
                tcQ.Controls.Add(lq);

                trQ.Cells.Add(tcQ);

                RadioButtonList rbl = new RadioButtonList();
                rbl.ID = "rbl" + i.ToString();
                rbl.RepeatDirection = RepeatDirection.Horizontal;

                if (DisabledValues)
                {
                    rbl.Enabled = false;
                }

                for (int j = Min; j <= Max; ++j)
                {
                    ListItem li = new ListItem();
                    li.Value = j.ToString();
                    li.Text = "";
                    rbl.Items.Add(li);
                    
                }

                if (savedQuestions[i].Evaluation_User_Question_Value.Count != 0)
                {
                    rbl.SelectedValue = savedQuestions[i].Evaluation_User_Question_Value.FirstOrDefault().Value.ToString();
                }
                TableCell tcCH = new TableCell();
                tcCH.Controls.Add(rbl);
                trQ.Cells.Add(tcCH);
                if (DisabledValues == false)
                {
                    //validator
                    RequiredFieldValidator rfv = new RequiredFieldValidator();
                    rfv.ID = "rfv" + i.ToString();
                    rfv.ErrorMessage = "*";
                    rfv.Display = ValidatorDisplay.Dynamic;
                    rfv.ControlToValidate = rbl.ID;
                    rfv.ForeColor = Color.Red;
                    TableCell tcCH2 = new TableCell();
                    tcCH2.Controls.Add(rfv);
                    trQ.Cells.Add(tcCH2);
                }
                t.Rows.Add(trQ);
            }

            PanelQuestions.Controls.Add(t);
        }

        /// <summary>
        /// Gets the evaluation values.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public List<Evaluation_User_Question_Value> GetEvaluationValues()
        {
            List<Evaluation_User_Question_Value> result = new List<Evaluation_User_Question_Value>();

            Table t = (Table)this.FindControl("TableQuestions");

            for (int i = 0; i < savedQuestions.Count; ++i)
            {
                Evaluation_User_Question_Value euqv = new Evaluation_User_Question_Value();
                euqv.Evaluation_Question_FK = savedQuestions[i].ID;

                RadioButtonList rbl = (RadioButtonList)t.FindControl("rbl" + i.ToString());
                euqv.Value = Int32.Parse(rbl.SelectedValue);

                result.Add(euqv);
            }
            return result;
        }

        /// <summary>
        /// Disables the values.
        /// </summary>
        /// <remarks></remarks>
        public void DisableValues()
        {
            Table t = (Table)this.FindControl("TableQuestions");

            foreach (Control c in t.Controls)
            {
                if (c.GetType().Name == "RadioButtonList")
                {
                    ((RadioButtonList)c).Enabled = false;
                }
            }
        }
    }
}    
    
